﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace PhysicsSimulator
{
    class CCandy : CGameObject
    {
        CSprite _candySprite;

        Vector2 _position = Vector2.Zero;
        public Vector2 Position
        {
            get
            {
                return _position;
            }
            set
            {
                _position = value;
            }
        }

        float _rotation = 0.0f;
        float _rotation_unit = 0.0f;
        float Y = 0.0f;

        Color _opacity = new Color(255, 255, 255, 255);

        public CCandy(SpriteBatch spritebatch)
            : base(spritebatch)
        {

        }

        public override void Init(Microsoft.Xna.Framework.Content.ContentManager contentManager)
        {
            base.Init(contentManager);
            Y = RandomHelper.Random.Next(360,415);
            _rotation_unit = (float)(RandomHelper.Random.NextDouble() - 0.5f)/ 10.0f;
            int idx = RandomHelper.Random.Next(1, 4);
            _candySprite = new CSprite(contentManager.Load<Texture2D>("Images/CandyGame/candy-"+idx.ToString()));
        }

        public override void Update(GameTimerEventArgs timer)
        {
            base.Update(timer);
            if (_position.Y < Y)
            {
                _position.Y += 9.5f;
                _rotation += _rotation_unit;
            }
            else
            {
                if (_opacity.A > 0)
                {
                    _opacity.R -= 1;
                    _opacity.G -= 1;
                    _opacity.B -= 1;
                    _opacity.A -= 1;
                }
                else
                {
                    _opacity.R = 0;
                    _opacity.G = 0;
                    _opacity.B = 0;
                    _opacity.A = 0;
                }
            }
        }

        public override void Draw(GameTimerEventArgs timer)
        {
            base.Draw(timer);
            SpriteBatch.Draw(_candySprite.Texture,_position,null,_opacity,_rotation,_candySprite.Origin,1,SpriteEffects.None,0);
        }
    }
}
